<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- 头部搜索区 -->
      <template #header>
        <el-form label-width="80px" inline size="normal">
          <el-form-item label="" size="normal">
            <el-input
              placeholder="请输入角色名称"
              size="normal"
              v-model="searchKeyword"
            ></el-input>
          </el-form-item>
          <el-form-item label="" size="normal">
            <el-button type="primary" icon="el-icon-search" @click="searchRole"
              >查询</el-button
            >
            <el-button @click="clearKeyword">清空</el-button>
          </el-form-item>
        </el-form>
      </template>
      <!-- 添加角色按钮 -->
      <div>
        <el-button
          type="primary"
          size="default"
          @click="isShowAddRoleDidlog"
          v-has="`btn.Role.add`"
          >添加</el-button
        >
      </div>
      <!-- 角色列表表格 -->
      <el-table border stripe style="margin-top: 20px" :data="roleList">
        <el-table-column type="selection" width="55" />
        <el-table-column label="序号" type="index" align="center" width="80px">
        </el-table-column>
        <el-table-column label="角色名称" align="center">
          <template #default="{ row }">
            <div v-if="row.isShow">
              <el-input
                v-model="row.roleName"
                placeholder=""
                size="normal"
                style="width: 80%"
                ref="roleRef"
              ></el-input>
              <el-button
                type="warning"
                size="small"
                style="margin-left: 15px"
                icon="el-icon-refresh"
                @click="cancelRole(row)"
                >取消</el-button
              >
            </div>
            <span v-else>{{ row.roleName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button
              type="info"
              size="small"
              icon="el-icon-info"
              @click="toAuth(row)"
              v-has="`btn.Role.assgin`"
            ></el-button>
            <el-button
              type="success"
              size="small"
              icon="el-icon-check"
              @click="upDataRole(row)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              v-if="!row.isShow"
              v-has="`btn.Role.update`"
              @click="toEdit(row)"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeRole(row)"
              v-has="`btn.Role.remove`"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px"
        :current-page="pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout=" prev, pager, next, jumper, -> ,sizes, total"
        :total="total"
        @current-change="changePageNo"
        @size-change="changePageSize"
      >
      </el-pagination>
    </el-card>
    <!-- 添加角色管理的区域 -->
    <el-dialog title="添加角色" width="30%" :visible.sync="isShowAddROle">
      <el-form label-width="80px">
        <el-form-item label="请输入新名称" label-width="120px">
          <el-input v-model="addRoleInput"></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button>取消</el-button>
        <el-button type="primary" @click="addRole">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// 引入请求
import {
  asyncGetRoleList,
  asyncAddRole,
  asyncUpDataRole,
  asyncDeleteRole,
} from "@/api/acl/role";
import { Message } from "element-ui";
// 引入深拷贝函数
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Role",
  data() {
    return {
      pageNo: 1,
      pageSize: 5,
      total: 20,
      // 角色列表
      roleList: [],
      //搜索的关键字
      searchKeyword: "",
      // 控制添加角色弹框的显示与隐藏
      isShowAddROle: false,
      // 添加角色的数据
      addRoleInput: "",
    };
  },
  methods: {
    // 请求角色列表的功能函数
    async getRoleList() {
      try {
        let res = await asyncGetRoleList(this.pageNo, this.pageSize);
        // console.log(res);
        this.total = res.data.total;
        this.roleList = res.data.items.map((item) => {
          item.isShow = false;
          return item;
        });
      } catch (error) {
        console.log(error);
      }
    },
    // 分页器的回调
    changePageNo(page) {
      this.pageNo = page;
      this.getRoleList();
    },
    // 改变一页显示个数的回调
    changePageSize(page) {
      this.pageSize = page;
      this.pageNo = 1;
      this.getRoleList();
    },
    // 查询角色的回调
    async searchRole() {
      const { pageNo, pageSize, searchKeyword } = this;
      try {
        let res = await asyncGetRoleList(pageNo, pageSize, searchKeyword);
        // console.log(res);
        this.total = res.data.total;
        this.roleList = res.data.items;
      } catch (error) {
        console.log(error);
      }
    },
    // 点击清空关键字的回调
    clearKeyword() {
      this.searchKeyword = "";
      this.getRoleList();
    },
    // 点击添加角色按钮的回调
    isShowAddRoleDidlog() {
      this.isShowAddROle = true;
    },
    // 点击添加页确认按钮的回调
    async addRole() {
      try {
        let res = await asyncAddRole(this.addRoleInput);
        // console.log(res);
        this.isShowAddROle = false;
        this.getRoleList();
      } catch (error) {
        console.log(error);
      }
    },
    //点击编辑按钮的回调
    toEdit(row) {
      row.isShow = true;
      this.$nextTick(() => {
        this.$refs.roleRef.focus();
      });
    },
    // 点击取消修改的回调
    cancelRole(row) {
      row.isShow = false;
      this.getRoleList();
      Message.warning("取消修改");
    },
    // 点击修改角色名称保存的回调
    async upDataRole(row) {
      let role = cloneDeep(row);
      try {
        let res = await asyncUpDataRole(role);
        // console.log(res);
        row.isShow = false;
        this.getRoleList();
        Message.success("修改成功");
      } catch (error) {
        console.log(error);
      }
    },
    // 点击删除按钮的回调
    async removeRole(row) {
      this.$confirm(`确定要删除${row.roleName}吗?`, "确认删除", {
        cancelButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            let res = await asyncDeleteRole(row.id);
            if (res.code == 20000) {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getRoleList();
            }
          } catch (error) {}
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消删除",
          });
        });
    },
    // 点击分配权限的回调
    toAuth(row) {
      this.$router.push({
        path: "/acl/role/auth",
        query: {
          roleId: row.id,
          username: row.roleName,
        },
      });
    },
  },
  /* 生命周期 */
  mounted() {
    this.getRoleList();
  },
};
</script>

<style>
</style>